package models;

import Dao.DBConnect;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CarProductModel  {

    int proId;
    String proName;
    Long proPrice;
    int proSid;
    DBConnect conn = null;
    Statement stmt = null;



    public CarProductModel() {
//        conn = new DBConnect();
    }


    public CarProductModel(int proId, String proName, Long proPrice) {
        this.proId = proId;
        this.proName = proName;
        this.proPrice = proPrice;
    }

    public int getProId() {
        return proId;
    }

    public int getProSid() {
        return proSid;
    }

    public void setProSid(int proSid) {
        this.proSid = proSid;
    }

    public void setProId(int proId) {
        this.proId = proId;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public Long getProPrice() {
        return proPrice;
    }

    public void setProPrice(Long proPrice) {
        this.proPrice = proPrice;
    }

    @Override
    public String toString() {
        return "Product{" +
                "proId=" + proId +
                ", proName='" + proName + '\'' +
                ", proPrice=" + proPrice +
                '}';
    }


    public List<CarProductModel> getAllProduct() {
        List<CarProductModel> products = new ArrayList<>();
        String query = "SELECT id, product_name,price , sid FROM mian_wang_product ;";
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(query)) {
//            statement.setInt(1, cid);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                CarProductModel product = new CarProductModel();
                // grab record data by table field name into ClientModel account object
                product.setProId(resultSet.getInt("id"));
                product.setProName(resultSet.getString("product_name"));
                product.setProPrice(resultSet.getLong("price"));
                product.setProSid(resultSet.getInt("sid"));
                products.add(product); // add account data to arraylist
            }
//            connection.close();
        } catch (SQLException e) {
            System.out.println("Error fetching Accounts: " + e);
        }
        System.out.println(products.toString());
        return products; // return arraylist
    }

    public List<CarProductModel> getAllProductwithShop(int sid) {
        List<CarProductModel> products = new ArrayList<>();
        String query = "SELECT id, product_name,price FROM mian_wang_product where sid = ? ;";
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(query)) {
            statement.setInt(1, sid);
            ResultSet resultSet = statement.executeQuery();
            System.out.println(statement.toString());
            while (resultSet.next()) {
                CarProductModel product = new CarProductModel();
                // grab record data by table field name into ClientModel account object
                product.setProId(resultSet.getInt("id"));
                product.setProName(resultSet.getString("product_name"));
                product.setProPrice(resultSet.getLong("price"));
                products.add(product); // add account data to arraylist
            }
//            connection.close();
        } catch (SQLException e) {
            System.out.println("Error fetching Accounts: " + e);
        }
//        System.out.println();
        System.out.println(products.toString());
        return products; // return arraylist
    }


    public void insertProduct(CarProductModel cps) {

        try {
//			setCid(cid);
            // Execute a query
            System.out.println("Inserting record into the product table...");
            stmt = DBConnect.getConnection().createStatement();
            String sql = null;

            sql = " insert into mian_wang_product(product_name, price ,sid) values('" + cps.getProName()  + "',  ' " + cps.getProPrice() +" ' , '" + cps.getProSid() + "')";

            stmt.executeUpdate(sql);


//            connection.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }


    public void updateProduct(CarProductModel cps ) {


        try {
            System.out.println("Update product table..." + cps.getProId() + "  " + cps.getProName() + "  " + cps.getProPrice() + "  " + cps.getProSid());
//            stmt = conn.getConnection().createStatement();
            String sql = null;

            sql = " update mian_wang_product set  product_name = ? , price = ? ,sid = ?  where id = ?";
            System.out.println(sql);
            try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(sql)) {
//            statement.setInt(1, cid);
                statement.setString(1,cps.getProName());
                statement.setInt(2, Math.toIntExact(cps.getProPrice()));
                statement.setInt(3, cps.getProSid());
                statement.setInt(4, cps.getProId());
                System.out.println(statement.toString());
                statement.executeUpdate();
            }
//            DBConnect.getConnection().close();

        } catch (SQLException se) {
            se.printStackTrace();
        }

    }

}
